package railRoad.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.BasicConfigurator;

import railRoad.web.slaves.*;
import railRoad.web.slaves.interfaces.ISlaveServlet;
import railRoad.worker.FactoryRW;

/**
 * Command servlet that calls implementation of {@link#ISlaveServlet}
 * @see HttpServlet
 * @see ISlaveServlet
 * @author Rukin
 *
 */
public class MainServlet extends HttpServlet {

	// auto generated
	private static final long serialVersionUID = -5399034700548498948L;

	private static final Logger log = Logger.getLogger(MainServlet.class
			.getName());

	private final Map<String, ISlaveServlet> slaves = 
		new HashMap<String, ISlaveServlet>();

	@Override
	public void init(javax.servlet.ServletConfig config)
			throws ServletException {
		super.init(config);
		BasicConfigurator.configure();

		FactoryRW.initDB("sa", "");
		FactoryRW.readDB();

		slaves.put("/index.do", new IndexServlet());
		slaves.put("/pages/login.do", new LoginServlet());
		slaves.put("/pages/logout.do", new LogoutServlet());
		slaves.put("/pages/register.do", new RegisterServlet());
		slaves.put("/pages/create_order.do", new CreateOrderServlet());
		slaves.put("/pages/station_list.do", new StationlistServlet());
		slaves.put("/pages/order_ticket.do", new TicketServlet());
		slaves.put("/pages/history.do", new HistoryServlet());
		slaves.put("/pages/confirm.do", new ConfirmOrderServlet());
		slaves.put("/pages/cancel.do", new CancelOrderServlet());
		slaves.put("/pages/route_remove.do", new RouteRemoveServlet());
		slaves.put("/pages/station_create.do", new StationAddServlet());
		slaves.put("/pages/station_edit.do", new StationEditServlet());
		slaves.put("/pages/station_upgrade.do", new StationUpgradeServlet());
		slaves.put("/pages/route_create.do", new RouteCreateServlet());
		slaves.put("/pages/route_edit.do", new RouteEditServlet());
		
		log.info("init() complete");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=UTF-8");
		
		String uri = req.getRequestURI();
		
		req.getSession(true).removeAttribute("cantLogin");
		req.getSession().removeAttribute("notAdmin");
		
		final ISlaveServlet slaveServlet = slaves.get(uri);
		if (slaveServlet != null) {
			log.info("GET '" + uri + "' --> " + slaveServlet.getClass());
			slaveServlet.work(req, resp);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=UTF-8");
		
		String uri = req.getRequestURI();
		
		req.getSession(true).removeAttribute("cantLogin");
		req.getSession().removeAttribute("notAdmin");
		
		final ISlaveServlet slaveServlet = slaves.get(uri);
		if (slaveServlet != null) {
			log.info("POST '" + uri + "' --> " + slaveServlet.getClass());
			slaveServlet.work(req, resp);
		}
	}

	@Override
	public void destroy() {
		super.destroy();
		FactoryRW.closeDB();

		log.info("destroy() complete");
	}
}
